﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Tasks;
using Windows.Storage;
using WP8Demos.Constants;
using WP8Demos.Helpers;
using WP8Demos.Interfaces;

namespace WP8Demos.ViewModels
{
    public class MainViewModel
    {
        private readonly INavigationService navigationService;

        private List<string> demoList = new List<string>() { "Sharing", "Live tiles", "Lock screen", "NotifyPropertyChanged example" };
        private Dictionary<string, Action> pagesActions;
        private ICommand tapCommand;

        private MainViewModel()
        {
        }

        public MainViewModel(INavigationService navigationService)
        {
            this.navigationService = navigationService;

            InitializeMainViewModel();
        }

        public List<string> DemoList
        {
            get
            {
                return demoList;
            }
        }

        public string TapCommandParameter
        {
            get;
            set;
        }

        public ICommand TapCommand
        {
            get
            {
                if (tapCommand == null)
                {
                    this.tapCommand = new RelayCommand<string>(TapCommandExecuted);
                }
                return tapCommand;
            }
        }

        private void InitializeMainViewModel()
        {
            this.pagesActions = new Dictionary<string, Action>()
            {
                {demoList[0], OpenSharing},
                {demoList[1], GoToTilesPage},
                {demoList[2], GoToLockPage},
                {demoList[3], GoToNotifyPage}
            };
        }

        #region Navigation
        private void GoToLockPage()
        {
            this.navigationService.Navigate(LocationStrings.VIEWS_LOCK);
        }
        #endregion

        private void GoToTilesPage()
        {
            this.navigationService.Navigate(LocationStrings.VIEWS_TILES);
        }

        private void GoToNotifyPage()
        {
            this.navigationService.Navigate(LocationStrings.VIEWS_NOTIFY_PROPERTY_CHANGED);
        }

        #region Tweeting
        private void OpenSharing()
        {
            var task = new ShareLinkTask();

            task.LinkUri = new Uri("http://www.skype.com", UriKind.Absolute);
            task.Message = "I ❤ Skype !";
            task.Title = "(/◔ ◡ ◔)/ ︵ ┻━┻";
            task.Show();
        }
        #endregion

        private void TapCommandExecuted(string selectedItem)
        {
            if (selectedItem != null)
            {
                this.pagesActions[selectedItem]();
            }
        }
    }
}
